package fy.hr.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;

import fy.config.FyConfig;
import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogReader;
import fy.hr.bean.MemberBean;
import fy.hr.dao.FindPersonInfoDao3;
import fy.hr.dao.LeaveForm_FindUserInfoDao;
import fy.hr.dao.LeaveInstead_FindUserInfoDao;
import fy.hr.dao.MQPersonInfoDao;
import fy.hr.dao.HTTPPersonInfoDao;
import fy.hr.dao.QueryMemberInfoFromHRDao;
import fy.hr.dao.RecommendFormDao;
import fy.hr.dao.RecruitmentPersonInfoDao;
import fy.hr.dao.WorkOverTimeFormDao;
import fy.hr.manager.WritePersonalDevelopmentPlansManager;
import fy.hr.reinstatement.ReinstatementManager;
import fy.hr.staffRedeploy.StaffRedeploymentManager;
import fy.hr.staffRedeploy.StaffingNeedsManager;
import fy.mdm.dao.MaterialCodeDao;
import fy.util.MyUtil;

public class OA2HRController备份 extends BaseController {
	private static Log log = LogFactory.getLog(OA2HRController.class);

	private final static String findPersonInfoByCodeJSP = "fy_view/oa2hr/away/findPersonInfoByCode";

	private final static String writePersonInfoJSP = "fy_view/oa2hr/away/writePersonInfo";

	private final static String writePersonalDevelopmentPlans = "fy_view/oa2hr/writePersonalDevelopmentPlans";

	private final static String staffRedeploymentJSP = "fy_view/oa2hr/staffRedeployment";

	private final static String staffingNeedsJSP = "fy_view/oa2hr/staffingNeeds";
	/// oatest/WebContent/WEB-INF/jsp/fy_view/oa2hr/Recruitment/recruitmentPersonInfo.jsp
	private final static String recruitmentPersonInfoByIDnumJSP = "fy_view/oa2hr/Recruitment/recruitmentPersonInfo";
	private final static String workOverTimePersonInfoByIDnumJSP = "fy_view/oa2hr/workOvertime/workOverTimePersonInfo";
	// private final static String reinstatement =
	// "fy_view/oa2hr/reinstatement";

	private final static String JSONJSP = "fy_view/oa2hr/JSON";

	private final static String LOGJSP = "fy_view/oa2hr/log/log";

	private final static String LEAVEFROMJSP = "fy_view/oa2hr/leaveForm";
	
	
	/**
	 * 获取填写人员内部调动申请单填写界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getStaffRedeploymentJSP(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;

		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(staffRedeploymentJSP);
			// String code =
			// MyOrgManager.getInstance().getMemberByID(AppContext.getCurrentUser().getId()).getCode();
			StaffRedeploymentManager.initStaffRedeployment(modelAndView);
		}
		return modelAndView;
	}

	/**
	 * 通过工号查询人员信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView querMemberInfoByCode(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(JSONJSP);
			response.setCharacterEncoding("utf-8");
			String code = request.getParameter("code");
			if (code == null || "".equals(code)) {
				modelAndView.addObject("jsonStr", "1");
			} else {
				try {
					MemberBean bean = QueryMemberInfoFromHRDao.queryMemberByCode(code);
					modelAndView.addObject("jsonStr", bean == null ? "3" : bean.toString());
				} catch (Exception e) {
					modelAndView.addObject("jsonStr", "2");
					log.error("查询员工信息异常", e);
				}
			}

		}
		return modelAndView;
	}

	/**
	 * 获取填写人员内部调动申请单填写界面中的子部门和当前部门岗位信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView staffRedeployment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			String operate = request.getParameter("operate");
			String id = request.getParameter("id");
			modelAndView = new ModelAndView(JSONJSP);
			response.setCharacterEncoding("utf-8");
			modelAndView.addObject("jsonStr", StaffRedeploymentManager.queryDepInfo(operate, id));
		}
		return modelAndView;
	}

	/**
	 * 填写人员需求表插件界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getStaffingNeedsJSP(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(staffingNeedsJSP);
			String code = MyOrgManager.getInstance().getMemberByID(AppContext.getCurrentUser().getId()).getCode();

			StaffingNeedsManager snm = new StaffingNeedsManager();

			snm.setMemberCode(code);

			snm.query();

			modelAndView.addObject("posts", snm.getPostOptions());
			modelAndView.addObject("postRankMap", snm.getPostRankMapJSON());
			modelAndView.addObject("postNumJSON", snm.getPostNumJSON());
		}
		return modelAndView;
	}
	
	public ModelAndView getFindPersonInfoJSP(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;

		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findPersonInfoByCodeJSP);
		}
		return modelAndView;
	}

	public ModelAndView getWritePersonalDevelopmentPlansJSP(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;
		User user = AppContext.getCurrentUser();
		if (user != null) {

			WritePersonalDevelopmentPlansManager wm = new WritePersonalDevelopmentPlansManager();

			wm.setUserID(user.getId());
			// wm.setDepId(user.getDepartmentId());
			// wm.setAccountID(user.getAccountId());

			wm.query();

			modelAndView = new ModelAndView(writePersonalDevelopmentPlans);

			modelAndView.addObject("post", wm.getPost());
			modelAndView.addObject("code", wm.getUserCode());
			modelAndView.addObject("unitshortname", wm.getUnitshortname());
			modelAndView.addObject("dep", wm.getDep());
			// modelAndView.addObject("managerID", wm.getLeaderID());
			// modelAndView.addObject("managerName", wm.getLeaderName());
		}
		return modelAndView;
	}

	public ModelAndView findPersonInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(writePersonInfoJSP);
		try {

			boolean success = false;

			String codes = request.getParameter("codes");

			String needDate = request.getParameter("needDate");
			
			String newAway = request.getParameter("newAway");
			
			//System.out.println(needDate+newAway);

			response.setCharacterEncoding("utf-8");

			if (AppContext.getCurrentUser() != null && codes != null && !"".equals(codes)) {
			
				codes = codes.replaceAll(".*([';]+|(--)+).*", " ");

				if (!"".equals(codes)) {

					String[] codesArray = codes.split(",");
					

					if (codesArray != null && codesArray.length != 0) {
					
						FindPersonInfoDao3 fid = new FindPersonInfoDao3();

						fid.setCodes(codesArray);
					    fid.setNeedTime("true".equals(needDate));
					    fid.setNewAway("true".equals(newAway));
						modelAndView.addObject("personInfoJSON", fid.findPersonInfo().toString());
						success = true;
					}
				}
			} 
			if (!success) {
				modelAndView.addObject("personInfoJSON", "{}");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
/*	public ModelAndView findPersonInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(writePersonInfoJSP);
		try {

			boolean success = false;

			String codes = request.getParameter("codes");

			String needDate = request.getParameter("needDate");

			response.setCharacterEncoding("utf-8");
			
			if (AppContext.getCurrentUser() != null && codes != null
					&& !"".equals(codes)) {

				codes = codes.replaceAll(".*([';]+|(--)+).*", " ");


				if (!"".equals(codes)) {

					String[] codesArray = codes.split(",");

					if (codesArray != null && codesArray.length != 0) {
						FindPersonInfoDao fid = new FindPersonInfoDao();

						fid.setCodes(codesArray);
						fid.setNeedTime("true".equals(needDate));

						modelAndView.addObject("personInfoJSON", fid
								.findPersonInfo().toString());

						success = true;
					}
				}

			}

			if (!success) {
				modelAndView.addObject("personInfoJSON", "{}");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
*/
	public ModelAndView getErrorLogJSP(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		User user = AppContext.getCurrentUser();
		if (user != null && "zongban".equals(user.getLoginName())) {
			modelAndView = new ModelAndView(LOGJSP);

			LogReader lr = new LogReader();

			lr.read(MyUtil.getRealPath(OA2HRController.class) + "../dao");
			lr.findFormInfo(FyConfig.getOA_DB_CONFIG());

			modelAndView.addObject("errorLog", lr.getLogJSON());
		}
		return modelAndView;
	}
//请假条填写
	public ModelAndView getLeaveFormJSP(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(LEAVEFROMJSP);
		User user = AppContext.getCurrentUser();
		if (user != null) {
			LeaveForm_FindUserInfoDao dao = new LeaveForm_FindUserInfoDao();
			dao.setMemberID(user.getId());
			if (dao.query()) {
				modelAndView.addObject("userName", dao.getUserName());
				modelAndView.addObject("code", dao.getCode());
				modelAndView.addObject("post", dao.getPost());
				modelAndView.addObject("dep", dao.getDep());
				modelAndView.addObject("reportDate", dao.getReportDate());
				modelAndView.addObject("remainDays", dao.getRemainDays());
				modelAndView.addObject("nowTime", MyUtil.getNowStr());
				modelAndView.addObject("success", "true");
			}
		}
		return modelAndView;
	}

	/**
	 * 查询复职信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView queryReinstatementInfo(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;
		User user = AppContext.getCurrentUser();
		if (user != null) {
			modelAndView = new ModelAndView(JSONJSP);
			response.setCharacterEncoding("utf-8");
			String operate = request.getParameter("operate");
			if (ReinstatementManager.QUERY_MEMBER_INFO_OPERATE.equals(operate)) {
				String memberCode = request.getParameter("memberCode");
				ReinstatementManager.queryInfo(modelAndView, user.getId(), memberCode);
			} else {
				String pk = request.getParameter("pk");
				ReinstatementManager.queryDepAndPost(modelAndView, pk, operate);
			}
		}
		return modelAndView;
	}

	/**
	 * 通过人员需求申请表单据编号查询表单信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView queryRequirementInfo(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView(JSONJSP);
		response.setCharacterEncoding("utf-8");
		User user = AppContext.getCurrentUser();
		if (user != null) {
			String number = request.getParameter("number");
			if (number != null && number.length() == 11) {
				modelAndView.addObject("jsonStr", RecommendFormDao.queryRequirementInfo(number));
			} else {
				modelAndView.addObject("jsonStr", "{success:false,data:'服务器获取的单据编号格式不对，请重试。'}");
			}
		} else {
			modelAndView.addObject("jsonStr", "{success:false,data:'未获取用户登录信息，请登录后再填写表单'}");
		}
		return modelAndView;
	}

	/**
	 * 通过人员需求申请表单据编号查询表单信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView queryRequirementPersonInfo(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView(JSONJSP);
		response.setCharacterEncoding("utf-8");
		User user = AppContext.getCurrentUser();
		if (user != null) {
			String number = request.getParameter("number");
			if (number != null && (number.length() == 15 || number.length() == 18)) {
				modelAndView.addObject("jsonStr", RecommendFormDao.queryRequirementPersonInfo(number));
			} else {
				modelAndView.addObject("jsonStr", "{success:false,data:'服务器获取的人员身份证格式不对，请重试。'}");
			}
		} else {
			modelAndView.addObject("jsonStr", "{success:false,data:'未获取用户登录信息，请登录后再填写表单'}");
		}
		return modelAndView;
	}

	/**
	 * 请假条 代填 获取人员信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	//请假代填
	public ModelAndView queryMemberInfo_Leave(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView modelAndView = new ModelAndView(JSONJSP);
		response.setCharacterEncoding("utf-8");
		User user = AppContext.getCurrentUser();
		if (user != null) {
			String code = MyOrgManager.getInstance().getMemberByID(user.getId()).getCode();
			if (MyUtil.strIsNotNull(code)) {
				String leaveMemberCode = request.getParameter("code");
				if (MyUtil.strIsNotNull(leaveMemberCode)) {
					LeaveInstead_FindUserInfoDao dao = new LeaveInstead_FindUserInfoDao();
					dao.setInsteadCode(code);
					dao.setLeaveMemberCode(leaveMemberCode);
					modelAndView.addObject("jsonStr", dao.query());
					dao = null;
				} else {
					modelAndView.addObject("jsonStr", "{success:false,data:'请假人工号不能为空。'}");
				}
			} else {
				modelAndView.addObject("jsonStr", "{success:false,data:'代填人的OA信息不完整，缺失人员工号，请联系子公司信息部添加。'}");
			}
		} else {
			modelAndView.addObject("jsonStr", "{success:false,data:'未获得登录人员信息，请登录后重试。'}");
		}

		return modelAndView;
	}

public ModelAndView getFindPersonInfoJSPbyidnum(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(recruitmentPersonInfoByIDnumJSP);
		}
		return modelAndView;
	}

	public ModelAndView idnumFindPersonInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView(writePersonInfoJSP);
		try {
			boolean success = false;
			String idnums = request.getParameter("idnums");
			//String  hrcontroller=request.getParameter("hrcontroller");
			//String usercontroller=request.getParameter("usercontroller");
			response.setCharacterEncoding("utf-8");
			//System.out.println("1111111"+idnums+hrcontroller+usercontroller);
		if (AppContext.getCurrentUser() != null && idnums != null && !"".equals(idnums)) {
			//if (idnums != null && !"".equals(idnums)) {
				RecruitmentPersonInfoDao rpd = new RecruitmentPersonInfoDao();
			//	rpd.setHrcontroller("true".equals(hrcontroller));
				//rpd.setUsercontroller("true".equals(usercontroller));
				//rpd.setIdnums(idnums);
				//System.out.println("1111111"+idnums);
				modelAndView.addObject("personInfoJSON", rpd.idnumfindPersonInfo(idnums).toString());
			//	System.out.println("1111111"+idnums);
				success = true;
			}
			if (!success) {
				modelAndView.addObject("personInfoJSON", "{}");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	//加班加点查人
	public ModelAndView getFindPersonInfoByCodeJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//System.out.println("************************1");
		ModelAndView modelAndView = null;
		
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(workOverTimePersonInfoByIDnumJSP);
		}
		return modelAndView;
	}
	//加班加点
	public ModelAndView jobNumFindPersonInfo(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView(writePersonInfoJSP);
			try {
				boolean success = false;
		String jobNums = request.getParameter("jobNums");
				response.setCharacterEncoding("utf-8");
		/*if (AppContext.getCurrentUser() != null) {
				MQPersonInfoDao mq=new MQPersonInfoDao();
				mq.setCodes(jobNums);*/

				if (AppContext.getCurrentUser() != null && jobNums != null && !"".equals(jobNums)) {
					
					jobNums = jobNums.replaceAll(".*([';]+|(--)+).*", " ");

				if (!"".equals(jobNums)) {

						String[] jobNumsArray = jobNums.split(",");

						if (jobNumsArray != null && jobNumsArray.length != 0) {
						
							MQPersonInfoDao mq=new MQPersonInfoDao();
							mq.setCodes(jobNumsArray);
				//for(int temp=0;jobNumsArray.length>temp;temp++){
				//modelAndView.addObject("personInfoJSON",mq.findPersonInfo(jobNumsArray[temp]).toString());
						
						modelAndView.addObject("personInfoJSON",mq.findPersonInfo().toString());
							success = true;}
			
				//}
						
				if (!success) {
					modelAndView.addObject("personInfoJSON", "{}");
				}
				}}} catch (Exception e) {
				e.printStackTrace();
			}
			return modelAndView;		
					
}}
